﻿using System.Linq;
using System.Text;
using System.IO;
using Xunit;

namespace MicroApplication.Tests.Builder
{
    /// <summary>
    /// 代码生成器
    /// 根据实体信息生成 仓储层、业务逻辑层、控制器代码
    /// </summary>
    public class CreateCode
    {
        [Fact]
        public void ByModel()
        {

            var create = new Creater("Business", "DriverEntity", "司机信息");

            create.CreateRepository(@"G:\Demo\A_Temp\MicroApplication\MicroApplication.Repository");
            create.CreateService(@"G:\Demo\A_Temp\MicroApplication\MicroApplication.Services");
            create.CreateController(@"G:\Demo\A_Temp\MicroApplication\MicroApplication.Web\Controllers");
        }
    }
    /// <summary>
    /// 代码生成器
    /// </summary>
    public class Creater
    {
        private string _modelName;
        private string _entityName;
        private string _entityShortName;
        private string _entityShortNameLow;
        private string _entityDesc;

        /// <summary>
        /// 构建函数
        /// </summary>
        /// <param name="model">模块</param>
        /// <param name="entity">实体</param>
        /// <param name="entity">实体描述</param>
        public Creater(string model, string entity, string desc)
        {
            _modelName = model;
            _entityName = entity;
            _entityDesc = desc;
            _entityShortName = entity.Replace("Entity", "");
            _entityShortNameLow = _entityShortName.First().ToString().ToLower() + _entityShortName.Substring(1);

        }

        /// <summary>
        /// 生成仓储层代码
        /// </summary>
        /// <param name="repositoryDir">保存路径</param>
        public void CreateRepository(string repositoryDir)
        {
            var dir = Path.Combine(repositoryDir, _modelName);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
                var entityDir = Path.Combine(dir, _entityShortName);
                if (!Directory.Exists(entityDir))
                {
                    Directory.CreateDirectory(entityDir);
                }

                var template = File.ReadAllText(@"Builder\Template\RepositoryInteface.txt");
                var content = template
                    .Replace("{{ModelName}}", _modelName)
                    .Replace("{{EntityShortName}}", _entityShortName)
                    .Replace("{{EntityName}}", _entityName);
                File.WriteAllText(Path.Combine(entityDir, $"I{_entityShortName}Repository.cs"), content, Encoding.UTF8);

                template = File.ReadAllText(@"Builder\Template\RepositoryClass.txt");
                content = template
                    .Replace("{{ModelName}}", _modelName)
                    .Replace("{{EntityShortName}}", _entityShortName)
                    .Replace("{{EntityName}}", _entityName);
                File.WriteAllText(Path.Combine(entityDir, $"{_entityShortName}Repository.cs"), content, Encoding.UTF8);
            }
        }
        /// <summary>
        /// 生成业务逻辑层代码
        /// </summary>
        /// <param name="serviceDir">保存路径</param>
        public void CreateService(string serviceDir)
        {
            var dir = Path.Combine(serviceDir, _modelName);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
                dir = Path.Combine(dir, _entityShortName);
                Directory.CreateDirectory(dir);
                var inputDir = Path.Combine(dir, "Input");
                var outputDir = Path.Combine(dir, "Output");
                Directory.CreateDirectory(inputDir);
                Directory.CreateDirectory(outputDir);

                CreateMapConfig(dir);
                CreateInput(inputDir);
                CreateOutput(outputDir);

                var template = File.ReadAllText(@"Builder\Template\ServiceInteface.txt");
                var content = template
                    .Replace("{{ModelName}}", _modelName)
                    .Replace("{{EntityShortName}}", _entityShortName)
                    .Replace("{{EntityDesc}}", _entityDesc)
                    .Replace("{{EntityName}}", _entityName);

                File.WriteAllText(Path.Combine(dir, $"I{_entityShortName}Service.cs"), content, Encoding.UTF8);

                template = File.ReadAllText(@"Builder\Template\ServiceClass.txt");
                content = template
                    .Replace("{{ModelName}}", _modelName)
                    .Replace("{{EntityShortName}}", _entityShortName)
                    .Replace("{{EntityShortNameLow}}", _entityShortNameLow)
                    .Replace("{{EntityName}}", _entityName);

                File.WriteAllText(Path.Combine(dir, $"{_entityShortName}Service.cs"), content, Encoding.UTF8);
            }
        }
        private void CreateMapConfig(string serviceDir)
        {
            var template = File.ReadAllText(@"Builder\Template\MapConfig.txt");
            var content = template
                .Replace("{{ModelName}}", _modelName)
                .Replace("{{EntityShortName}}", _entityShortName)
                .Replace("{{EntityName}}", _entityName);

            File.WriteAllText(Path.Combine(serviceDir, "_MapConfig.cs"), content, Encoding.UTF8);
        }
        private void CreateInput(string dir)
        {
            var template = File.ReadAllText(@"Builder\Template\AddInput.txt");
            var content = template
                .Replace("{{ModelName}}", _modelName)
                .Replace("{{EntityShortName}}", _entityShortName)
                .Replace("{{EntityName}}", _entityName);

            File.WriteAllText(Path.Combine(dir, $"{_entityShortName}AddInput.cs"), content, Encoding.UTF8);

            template = File.ReadAllText(@"Builder\Template\UpdateInput.txt");
            content = template
                .Replace("{{ModelName}}", _modelName)
                .Replace("{{EntityShortName}}", _entityShortName)
                .Replace("{{EntityName}}", _entityName);

            File.WriteAllText(Path.Combine(dir, $"{_entityShortName}UpdateInput.cs"), content, Encoding.UTF8);
        }
        private void CreateOutput(string dir)
        {
            var template = File.ReadAllText(@"Builder\Template\ListOutput.txt");
            var content = template
                .Replace("{{ModelName}}", _modelName)
                .Replace("{{EntityShortName}}", _entityShortName)
                .Replace("{{EntityName}}", _entityName);

            File.WriteAllText(Path.Combine(dir, $"{_entityShortName}ListOutput.cs"), content, Encoding.UTF8);
        }
        /// <summary>
        /// 生成控制器代码
        /// </summary>
        /// <param name="controllerDir">保存路径</param>
        public void CreateController(string controllerDir)
        {
            var dir = Path.Combine(controllerDir, _modelName);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            var template = File.ReadAllText(@"Builder\Template\Controller.txt");
            var content = template
                 .Replace("{{ModelName}}", _modelName)
                 .Replace("{{EntityShortName}}", _entityShortName)
                 .Replace("{{EntityShortNameLow}}", _entityShortNameLow)
                 .Replace("{{EntityDesc}}", _entityDesc)
                 .Replace("{{EntityName}}", _entityName);

            File.WriteAllText(Path.Combine(dir, $"{_entityShortName}Controller.cs"), content, Encoding.UTF8);
        }
    }
}
